﻿using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using EPiServer;
using EPiServer.Core;

namespace EPiMVC.WebForms
{
    public class MvcUserControlBase : UserControl, IPageSource
    {
        public PageData CurrentPage
        {
            get
            {
                if (HttpContext.Current.CurrentHandler is MvcHandler)
                    return DataFactory.Instance.GetPage(
                        (PageReference)
                        ((MvcHandler)HttpContext.Current.CurrentHandler).RequestContext.RouteData.Values[
                            "CurrentPageReference"]
                        );
                
                return ((PageBase) this.Page).CurrentPage;
            }
        }

        public string Translate(string key)
        {
            return LanguageManager.Instance.Translate(key);
        }


        public PageDataCollection GetChildren(PageReference pageLink)
        {
            return DataFactory.Instance.GetChildren(pageLink);
        }

        public PageData GetPage(PageReference pageLink)
        {
            return DataFactory.Instance.GetPage(pageLink);
        }
    }
}
